#pragma once

#include <opencv2/opencv.hpp> 
using namespace cv;

/// <summary>
/// 数据类型转换和归一化
/// </summary>
/// <param name="image"></param>
void norm_demo(Mat& image)
{
	Mat dst; //默认CV_8UC3
	imshow("原图", image);
	std::cout << image.type() << std::endl;
	image.convertTo(image, CV_32F);	//转为32浮点型
	//image.convertTo(image, CV_32S);   //转为32整型 注意整型数据无法归一化 否则将成为全零黑图
	//格式转换后需要进行归一化才能正常显示
	normalize(image, dst, 1.0, 0, NORM_MINMAX);
	std::cout << dst.type() << std::endl;
	imshow("图像数据归一化", dst);
}


/// <summary>
/// 图像缩放和插值  
/// </summary>
/// <param name="image"></param>
void resize_demo(Mat& image)
{
	Mat zoomin, zoomout;
	int h = image.rows;
	int w = image.cols;

	resize(image, zoomin, Size(w / 2, h / 2), 0, 0, INTER_CUBIC);
	imshow("zoomin", zoomin);

	resize(image, zoomout, Size(w / 0.25, h / 0.25), 0, 0, INTER_CUBIC);
	imshow("zoomout", zoomout);
}


/// <summary>
/// 图像反转
/// </summary>
/// <param name="image"></param>
void flip_demo(Mat& image)
{
	Mat dst;
	flip(image, dst, 0);//		0上下翻转  
	flip(image, dst, 1);//		1 左右翻转    
	flip(image, dst, -1);//		-1对角线翻 转180

	imshow("flip", dst);
}


/// <summary>
/// 图像旋转
/// </summary>
/// <param name="image"></param>
void  rotate_demo(Mat& image)
{
	Mat dst, M;
	int h = image.rows;
	int w = image.cols;
	//若需要先将图面缩小则重获得缩小图的长宽
	resize(image, dst, Size(w / 2, h / 2), 0, 0, INTER_CUBIC);
	h = dst.rows;
	w = dst.cols;

	//api内置旋转矩阵生成
	M = getRotationMatrix2D(Point2f(w / 2, h / 2), 45, 1.0);
	double cos = abs(M.at<double>(0, 0));
	double sin = abs(M.at<double>(0, 1));
	int nw = cos * w + sin * h;
	int nh = sin * w + cos * h;

	//将旋转之后的中心和原图中心之间的偏移量预先算出加到矩阵的位移一栏里
	M.at<double>(0, 2) += (nw / 2 - w / 2);
	M.at<double>(1, 2) += (nh / 2 - h / 2);

	warpAffine(dst, dst, M, Size(nw, nh), INTER_LINEAR, 0, Scalar(0, 200, 0));

	imshow("rotate", dst);
}
